<?php
/*
	会员管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$merchid=$this->checklogin();
$return['merch_types']=$this->merch_types;
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
	);
	$list=$this->get_list('catefans',$parameter,$total);
	if($list){
		foreach($list as $k=>&$item){
			//$item['member']=cfc('core')->member_show($item['mid']);
			$item['member']=model('jy_merch/member')->member_show($item['mid']);
			if(!$item['member']['id']){
				pdo_delete('jy_merch_catefans',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));	
				unset($list[$k]);
			}
			$item['from_os']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'from_os');
			$item['from_os']=$item['from_os']?get_ostitle()[$item['from_os']]:'微信';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'<red>[温馨提示]：同一客户，可以跟不同类型的商户绑定推广关系；同一类型不能重复！</red>','key'=>'mid','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'mid','title'=>'ID','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'merch_types'),
			array('name'=>'from_os','title'=>'来源','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'sendcoupon','title'=>'发优惠券'),
			array('type'=>'modal','op'=>'sendmsg','title'=>'发消息'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'jy_merch/get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	$return['member_status']=$this->member_status;
	exi($return);
}
if($_RQ['op']=='sendcoupon'){
	if($_RQ['postdata']){
		$data=get_postdata();
		$num=intval($data['num'])?:1;
		$mid=intval($data['mid']);
		if($num>20){
			exi('每人最多发放20张！','error');
		}
		if(!$mid){
			exi('请选择会员！','error');
		}
		$data1=array('mid'=>$mid,'merchid'=>$merchid,'status'=>0,'uniacid'=>$_SESSION['uniacid'],'couponid'=>$data['coupon'],'getfrom'=>0,'createtime'=>TIMESTAMP);
		$data2=array('mid'=>$mid,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'couponid'=>$data['coupon'],'gettype'=>1,'createtime'=>TIMESTAMP);
		for($i=1;$i<=$num;$i++){
			pdo_update('jy_weishop_coupon',array('last +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$data['coupon']));
			pdo_insert('jy_weishop_coupon_getlog',$data2);
			pdo_insert('jy_weishop_coupon_log',$data1);
		}	
		$this->model('notice')->sendcoupon($mid);
		exi('发送成功！','','//list/');
	}
	$mid=intval($_RQ['mid']);
	$item=array('mid'=>$mid);
	$item['t_time'] =$item['t_time']?date('Y/m/d H:i',$item['t_time']):'';
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'发送优惠券',
		'edit'=>array(
			array('type'=>'copy','name'=>'mid','title'=>'会员MID'),		
			array('type'=>'select','name'=>'coupon','title'=>'选择优惠券','options'=>'coupon'),			
			array('type'=>'string','name'=>'num','title'=>'每人发放数量','help'=>'此处受总数限制，如果剩余张数不足以发放给选定会员数量，则无法发放'),	
		)
	);
	$return['htmls']=$htmls;
	$return['coupon']=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'));	
	exi($return);
}
if($_RQ['op']=='sendmsg'){
	if($_RQ['postdata']){
		$data=get_postdata();
		$mid=intval($data['mid']);
		if(!$data['content']){
			exi('请填写要发送的消息内容！','error');
		}
		if(!$mid){
			exi('请选择会员！','error');
		}
		$data1=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'merchid'=>$merchid,
			'status'=>0,
			'content'=>$data['content'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_merch_msglog',$data1);
		$this->model('notice')->sendmsg($mid,$data['content']);
		exi('发送成功！','','//list/');
	}
	$mid=intval($_RQ['mid']);
	$item=array('mid'=>$mid);
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'发送消息',
		'edit'=>array(
			array('type'=>'copy','name'=>'mid','title'=>'会员MID'),		
			array('type'=>'text','name'=>'content','title'=>'消息内容'),	
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
?>